Passed
Push — feature/data-hook-error-toast ( 5e187f...e4872b )
by Tristan
04:43
created

ErrorToast.tsx ➔ runCallbackIfValueUnchanged   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.95
c 0
b 0
f 0
cc 2
1
import React, { useCallback, useContext, useEffect, useRef } from "react";
2
import { defineMessages, useIntl } from "react-intl";
3
import { ErrorContext } from "./ErrorContainer";
4
import Alert from "./H2Components/Alert";
5
6
const TOAST_SELF_DISMISS_TIMER = 3500;
7
8
export const errorMessages = defineMessages({
9
  toastTitle: {
10
    id: "errorToast.title",
11
    defaultMessage: "Something went wrong!",
12
    description: "Title displayed on the Error Toast component.",
13
  },
14
  dismissLabel: {
15
    id: "errorToast.dismiss",
16
    defaultMessage: "Dismiss",
17
    description: "Label for the Error Toast dismiss button.",
18
  },
19
});
20
21
export const ErrorToast: React.FC = () => {
22
  const intl = useIntl();
23
  const { state, dispatch } = useContext(ErrorContext);
24
  const dismiss = useCallback(() => dispatch({ type: "pop" }), [dispatch]);
25
26
  // This toast will render the first error in the queue, if any.
27
  const currentError = state.errorQueue.length > 0 ? state.errorQueue[0] : null;
28
29
  return (
30
    <>
31
      {currentError !== null && (
32
        <Alert
33
          color="stop"
34
          position="toast"
35
          data-h2-radius="b(round)"
36
          data-h2-padding="b(all, .25)"
37
          dismissBtn={
38
            <Alert.DismissBtn
39
              onClick={dismiss}
40
              data-h2-padding="b(all, .25)"
41
              aria-label={intl.formatMessage(errorMessages.dismissLabel)}
42
            >
43
              <i className="fa fa-times-circle" />
44
            </Alert.DismissBtn>
45
          }
46
        >
47
          <Alert.Title>
48
            <strong>{intl.formatMessage(errorMessages.toastTitle)}</strong>
49
          </Alert.Title>
50
          <p>{currentError}</p>
51
        </Alert>
52
      )}
53
    </>
54
  );
55
};
56
57
export default ErrorToast;
58